//
//  EditPassWordVC.m
//  New_Runway
//
//  Created by mack on 2018/2/1.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "EditPassWordVC.h"
#import "PasswordCell.h"

@interface EditPassWordVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) UIButton *singOut;

@end

@implementation EditPassWordVC
{
    NSString *_formerP;
    NSString *_newP;
    NSString *_againP;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"修改密码";
    self.view.backgroundColor = kColorWhite();
    [self initTableview];
    _formerP = _newP = _againP = @"";
}

-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = UIColorHex(0xf5f5f5);
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"PasswordCell" bundle:nil] forCellReuseIdentifier:@"PasswordCell"];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_equalTo(self.navH );
        make.bottom.equalTo(weakSelf.view).mas_equalTo(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_width, 200)];
    self.tableview.tableFooterView = footerView;
    footerView.backgroundColor =[UIColor clearColor];
    UIButton *singOut = [[UIButton alloc] initWithFrame:CGRectMake(27.5, 60, Screen_width - 55, 40)];
    [singOut setBackgroundImage:[UIImage imageWithColor:UIColorHex(0xb4b4b4)] forState:UIControlStateNormal];
    [singOut setBackgroundImage:[UIImage imageWithColor:UIColorHex(0xb03e00)] forState:UIControlStateHighlighted];
    [singOut setTitleColor:kColorWhite() forState:UIControlStateNormal];
    singOut.userInteractionEnabled = NO;
    
    singOut.layer.masksToBounds = YES;
    singOut.layer.cornerRadius = 5;
    [singOut setTitle:@"确  定" forState:UIControlStateNormal];
    [singOut.titleLabel setFont:kFontSize(16)];
    [singOut addTarget:self action:@selector(changePassword) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:singOut];
    self.singOut = singOut;
    
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PasswordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PasswordCell"];
    cell.inputTF.delegate = self;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        cell.titleL.text = @"原始密码";
        cell.inputTF.placeholder = @"请输入原始密码";
        cell.inputTF.tag = 1;
    }else{
        if (indexPath.row == 0) {
            cell.inputTF.placeholder = @"请输入新密码";
            cell.titleL.text = @"新密码";
            cell.inputTF.tag = 2;

        }else{
            cell.inputTF.placeholder = @"请输再次入新密码";
            cell.titleL.text = @"确认密码";
            cell.inputTF.tag = 3;

        }
    }
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColorHex(0xf5f5f5);
    return view;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColorHex(0xf5f5f5);
    return view;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)changePassword{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//关闭键盘
    [self updataPassword];
    
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
   
    if (textField.tag == 1) {
        _formerP = text;
       
    }else if (textField.tag == 2){
        _newP = text;
        
    }else if (textField.tag == 3){
        _againP = text;
       
    }
    
    
    if (![_formerP isEqualToString:@""] && ![_newP isEqualToString:@""] && ![_againP isEqualToString:@""]) {
        [self.singOut setBackgroundImage:[UIImage imageWithColor:MAIN_NAV_COLOR()] forState:UIControlStateNormal];
        self.singOut.userInteractionEnabled = YES;
    }else{
        [self.singOut setBackgroundImage:[UIImage imageWithColor:UIColorHex(0xb4b4b4)] forState:UIControlStateNormal];
        self.singOut.userInteractionEnabled = NO;
    }
    return YES;
}

-(void)updataPassword{

    if ([_formerP isEqualToString:@""]) {
        [ShowMessage showHintMessage:@"请输入原始密码!" duration:2.0 withRemindId:remindWarning];

        return;
    }
    if ([_newP isEqualToString:@""]) {
        [ShowMessage showHintMessage:@"请输入新密码!" duration:2.0 withRemindId:remindWarning];
        return;
    }
    if ([_againP isEqualToString:@""]) {
        [ShowMessage showHintMessage:@"请再次输入新密码!" duration:2.0 withRemindId:remindWarning];

        return;
    }
    if (![_newP isEqualToString:_againP]) {
        [ShowMessage showHintMessage:@"新密码与确认密码不统一!" duration:2.0 withRemindId:remindWarning];

        return;
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,updataPWDApi];
    NSDictionary *params = @{
                             @"accountPassword":_formerP,
                             @"newPassword":_newP,
                             @"accountName":[NRWCarUser sharedUser].accountName
                             };
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        if ([response[@"code"] isEqual:@1]) {
            [self.navigationController popViewControllerAnimated:YES];
            [ShowMessage showHintMessage:@"修改成功!" duration:2.0 withRemindId:remindSuccess];

        }else{
            [ShowMessage showHintMessage:response[@"msg"] duration:2.0 withRemindId:remindFail];


        }
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
